﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace iPodBrake
{
    class Queue
    {
        string _fileName;
        FileInfo _fi;

        public Queue(string VideoFile)
        {
            _fileName = VideoFile;
            _fi = new FileInfo(VideoFile);
        }

        public string FileName
        {
            get
            {
                return _fi.Name;
            }
        }
        public string FileNameWOExtension
        {
            get
            {
                string name = _fi.Name;
                int ext = _fi.Extension.Length;
                name = name.Remove(name.Length - ext);
                return name;
            }

        }
        public string Path
        {
            get
            {
                return _fi.DirectoryName;
            }
        }
        public string SrtFileName
        {
            get
            {
                string srt = Path + "\\" + FileNameWOExtension + ".srt";
                if (!File.Exists(srt))
                {
                    DirectoryInfo di = new DirectoryInfo(Path);
                    string Season = di.Name;
                    string Show = di.Parent.Name;
                    Bierdopje.API.ApiLibrary bd = new Bierdopje.API.ApiLibrary("0C488FF8DAFDCEF2");
                    List<Bierdopje.API.ApiShow> showResults = bd.FindShowByName(Show);
                    Bierdopje.API.ApiShow found = null;
                    foreach (Bierdopje.API.ApiShow result in showResults)
                    {
                        if (result.Name == Show)
                        {
                            found = result;
                            break;
                        }
                    }
                    if (found == null)
                    {
                        return "";
                    }
                    Season = Season.Remove(0, 7);
                    string Episode = this.FileName;
                    Episode = Episode.Remove(0, Episode.IndexOf("x"));
                    string[] splitter = Episode.Split(new string[] { " " },1, StringSplitOptions.None);
                    Episode = splitter[0];

                    Bierdopje.API.ApiEpisode episode = found.GetEpisode(int.Parse(Season), int.Parse(Episode));
                    return "";
                }
                else
                {
                    return srt;
                }
            }
        }
        public bool SrtExists
        {
            get
            {
                return File.Exists(SrtFileName);
            }
        }
        public string OutputFileName
        {
            get
            {
                return Path + "\\" + FileNameWOExtension + ".m4v";
            }

        }
        public string HandBrakeArguments
        {
            get
            {
                string arguments = "";
                // input file
                arguments = arguments + "-i \"" + Path + "\\" + FileName + "\"";

                // output file
                arguments = arguments + " -o \"" + OutputFileName + "\"";

                // Preset
                arguments = arguments + " --preset=\"iPhone & iPod Touch\"";

                // Add a subtitle if there is one
                if (SrtExists)
                {
                    // Preset
                    arguments = arguments + " --srt-file \"" + SrtFileName + "\"";
                }
                return arguments;
            }
        }
        public bool Exists
        {
            get
            {
                return File.Exists(this.Path + "\\" + this.FileName);
            }
        }
        public bool OutputExists
        {
            get
            {
                return File.Exists(this.OutputFileName);
            }
        }
    }
}
